package ch.sfug.log {
	import ch.sfug.log.output.ILoggerOutput;			    

	/**
	 *
	 */
	public class Logger {

		private static var instance:LoggerInstance;

		public static const DEFAULT:String = "default";

		
		public static function clear( scope:String = DEFAULT ):void {
			getInstance().clear(scope);
		}

		public static function log( obj:*, scope:String = DEFAULT ):void {
			getInstance().log(obj, scope);
		}

		
		public static function appendOutput( op:ILoggerOutput, scope:String = DEFAULT ):void {
			getInstance().append(op, scope);
		}

		private static function getInstance():LoggerInstance {
			if( instance == null ) {
				instance = new LoggerInstance();
			}
			return instance;
		}
	}
}

import flash.utils.Dictionary;

import ch.sfug.log.output.ILoggerOutput;

/**
 * @author mich
 */
class LoggerInstance {

	
	private var dict:Dictionary;

	function LoggerInstance() {
		dict = new Dictionary();
	}

	internal function log( txt:String, scope:String ):void {
		txt = getTime() + txt;
		var outputs:Array = dict[ scope ];
		if( outputs != null ) {
			for (var i:Number = 0;i < outputs.length; i++) {
				var output:ILoggerOutput = ILoggerOutput(outputs[ i ]);
				output.append(txt);
			}
		}
	}

	
	internal function append( op:ILoggerOutput, scope:String ):void {
		if( dict[ scope ] == null ) dict[ scope ] = new Array();
		dict[ scope ].push(op);
	}

	
	internal function clear( scope:String ):void {
		var outputs:Array = dict[ scope ];
		if( outputs != null ) {
			for (var i:Number = 0;i < outputs.length; i++) {
				var output:ILoggerOutput = ILoggerOutput(outputs[ i ]);
				output.clear();
			}
		}
	}
	
	private function getTime():String {
		var d:Date = new Date();
		return checkFor2Digits( d.hours ) + ":" + checkFor2Digits( d.minutes ) + ":" + checkFor2Digits( d.seconds ) + " : ";
	}
	
	private function checkFor2Digits( num:Number ):String {
		if( num < 10 ) {
			return "0" + num;
		} else {
			return num.toString();
		}
	}
}
